// pages/home/home.js
import { bannerArr, getCase, getActive, getDoctor, login, getChannel, getList, getArcList, homeGetTel} from '../../utils/api.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin:false,
    tel: '',
    animation: { duration: 2 },
    swiperList: [],
    gridList: [],
    caseParams: {
      page: 1,
      pageSize: 10,
      recommend_type: 1
    },
    activeParams: {
      page: 1,
      pageSize: 10,
      recommend_type: 1
    },
    activeList: [],
    caseList: [],
    doctorList: [],
    active: 0,
    currentId:0,
    channelList: [],
    newsList:[],
    model1:'',
    model2: '',
    model3: '',
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none',
    });
  },
  toArcDetail(e){
    let info = e.currentTarget.dataset.info
    if (info.arc_id&&info.list_id){
      wx.navigateTo({
        url: '/pages/detailed/detailed?id=' + info.arc_id + '&list_id=' + info.list_id,
      })
    }
  },
  getPhoneNumber(e){
    homeGetTel(e)
  },
  register(e){
    if (this.data.isLogin){
      wx.navigateTo({
        url: '/pages/appointment/appointment?id=' + e.currentTarget.dataset.id,
      })
    }else{
      login(true,'/pages/appointment/appointment?id=' + e.currentTarget.dataset.id)
    }
    
  },
  getsph(e) {
    let video_id= e.currentTarget.dataset.info.video_id
    let videoId = wx.getStorageSync('videoId')
    wx.openChannelsActivity({
      finderUserName: videoId, 
      feedId: video_id,
      success: (res) => {
        console.log(res)
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },
  openDetail(e){
    if(e.currentTarget.dataset.relation_id==1){
      wx.navigateTo({      
        url: '/pages/doctorlist/doctorlist?navbar=1&navbar_id=' + e.currentTarget.dataset.navbar_id,
      })
    } else if (e.currentTarget.dataset.relation_id==3){
      wx.navigateTo({
        url: '/pages/caselist/caselist?navbar=1&navbar_id=' + e.currentTarget.dataset.navbar_id,
      })
    } else if (e.currentTarget.dataset.relation_id == 2) {
      wx.navigateTo({
        url: '/pages/recoveryList/recoveryList?navbar=1&navbar_id=' + e.currentTarget.dataset.navbar_id,
      })
    }
    
  },
  toList(e){
    getChannel().then(res => {     
      res.data.forEach(item => {
        item.children.unshift({ name: '全部', id: item.id })
      })
      wx.navigateTo({
        url: '/pages/list/list?id=' + e.currentTarget.dataset.id + '&info=' + encodeURIComponent(JSON.stringify(res.data)),
      })
    })
    
  },
  getCaseList(){
    getCase(this.data.caseParams).then(res => {
      this.setData({
        caseList: res.data.list,
        model1: res.data.module_name
      })
      wx.setStorageSync('model1', res.data.module_name)
    })
  },
  getActiveList(){
    getActive(this.data.activeParams).then(res => {      
      this.setData({
        activeList: res.data.list,
        model2: res.data.module_name
      })
      wx.setStorageSync('model2', res.data.module_name)
    })
  },
  getListData() {
    getArcList().then(res => {        
      res.data.forEach(item=>{
        let all = item.children.pop()
        item.children.unshift(all)
      })
      this.setData({
        newsList:res.data
      })
    })
  },
  getDoctorList(){
    getDoctor().then(res => {
      this.setData({
        doctorList: res.data.list,
        model3: res.data.module_name
      })
      wx.setStorageSync('model3', res.data.module_name)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {    
    bannerArr().then(res => {
      wx.setStorageSync('videoId', res.data.global_config.videoId)
      this.setData({
        swiperList: res.data.ad,
        gridList: res.data.navbar
      })
    })
      // 案例
      this.getCaseList()
      // 活动
      this.getActiveList()
      // 医生
      this.getDoctorList()
      // 文章
      this.getListData()
  },
 

  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (wx.getStorageSync('tel')){
      this.setData({
        tel: wx.getStorageSync('tel')
      })
    }
    if (wx.getStorageSync('isLogin')) {
      this.setData({
        isLogin: wx.getStorageSync('isLogin')
      })
    } else {
      this.setData({
        isLogin: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    bannerArr().then(res => {
      this.setData({
        swiperList: res.data.ad,
        gridList: res.data.navbar
      })
      wx.stopPullDownRefresh()
    })
    // 案例
    this.getCaseList()
    // 活动
    this.getActiveList()
    // 医生
    this.getDoctorList()
    // 文章
    this.getListData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})